package tgc.edu.tgq.bootstrap.web.controller;


import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.tgq.bootstrap.custom.AjaxResult;
import tgc.edu.tgq.bootstrap.custom.TablePageable;
import tgc.edu.tgq.bootstrap.entity.Course;
import tgc.edu.tgq.bootstrap.entity.Grade;
import tgc.edu.tgq.bootstrap.entity.Student;
import tgc.edu.tgq.bootstrap.entity.SysUser;
import tgc.edu.tgq.bootstrap.entity.TbClass;
import tgc.edu.tgq.bootstrap.security.UserUtils;
import tgc.edu.tgq.bootstrap.service.CourseService;
import tgc.edu.tgq.bootstrap.service.GradeService;
import tgc.edu.tgq.bootstrap.service.StudentService;
import tgc.edu.tgq.bootstrap.service.TbClassService;
import tgc.edu.tgq.bootstrap.web.form.GradeForm;

@Controller
@RequestMapping(value = "/grade")
public class GradeController {
	@Autowired
	private GradeService gradeService;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private StudentService studentService;
	@Autowired
	private TbClassService tbClassService;
	@Autowired
	private CourseService courseService;

	@RequestMapping(value = "/classcourse")
	public void classcourse(ModelMap modelMap) {
	}
	@RequestMapping(value = "/classgrade")
	public void classgrade(ModelMap modelMap) {
	}
	@RequestMapping(value = "/selfgrade")
	public void selfgrade(ModelMap modelMap) {
	}
	
	
	// ----------个人成绩查询------------------
	@RequestMapping(value = "/personGrade")
	public String personGrade(ModelMap modelMap) {
		SysUser user = userUtils.getUser();
		Integer id = user.getId();
		List<Grade> list = gradeService.findByStudentId(id);
		modelMap.put("list", list);
		return "grade/personGrade";
	}

	// -------------通过id查找学生---------------
	@RequestMapping(value = "/classGrade")
	public String classGrade(ModelMap modelMap) {
		SysUser user = userUtils.getUser();
		Integer id = user.getId();
		Student student = studentService.findById(id);
		modelMap.put("stu", student);
		return "grade/classGrade";
	}
	
	// -----------班级成绩查询-------------------
	@RequestMapping(value = "/allStudentGrade")
	public String list(ModelMap modelMap) {
		List<TbClass> tbClass = tbClassService.findAll();
		modelMap.put("list", tbClass);
		return "grade/allStudentGrade";
	}
	//------------班级成绩录入--------------
	@RequestMapping(value = { "/classcourse" }, method = RequestMethod.POST)
	@ResponseBody
	public Object classcourse(ModelMap modelmap, Integer bjId, Integer couId) {
		List<Grade> grade = gradeService.findByTbClassIdAndCourseId(bjId, couId);
		modelmap.put("grade", grade);
		return grade;
	}
	@SuppressWarnings("serial")
	public Specification<Grade> buildSpec(Integer bjId,Integer kcId) {
		Specification<Grade> spec=new Specification<Grade>() {

			@Override
			public Predicate toPredicate(Root<Grade> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(bjId!=null) {
					rules.add(cb.equal(root.get("student").get("tbClass").get("id").as(String.class), bjId));
				}
				if(kcId!=null) {
					rules.add(cb.equal(root.get("course").get("id").as(String.class), kcId));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	@RequestMapping(value = "/page")
	@ResponseBody
	public Object page(TablePageable tablePaegable,Integer bjId,Integer kcId) {
		List<Student> students=studentService.findByTbClassId(bjId);
		for(Student s:students) {
		Grade grade=gradeService.findByStudentIdAndCourseId(s.getId(), kcId);
		if(grade==null) {
			Grade grade1=new Grade();
			grade1.setCourse(courseService.findById(kcId));
			grade1.setStudent(studentService.findById(s.getId()));
			grade1.setGrade(null);
			gradeService.save(grade1);
			}
		}
		PageRequest pageable = tablePaegable.buildPageRequest();
		Specification<Grade> spec = buildSpec(bjId,kcId);
		Page<Grade> page = gradeService.findAll(spec, pageable);
		return AjaxResult.buildTableResult(page);
	}
	//------------个人成绩录入---------------
	@RequestMapping(value = "/stu")
	@ResponseBody
	public Object getStu(Integer  bjId,ModelMap modelMap) {
		List<Student> students=studentService.findByTbClassId(bjId);
		return students;
	}
	@RequestMapping(value = "/getGrade")
	@ResponseBody
	public Object getGrade(Integer stuId,ModelMap modelMap) {
		List<Grade> grades=gradeService.findByStudentId(stuId);
		modelMap.put("list",grades);
		return grades;
	}
	@RequestMapping(value = { "/selfgrade" }, method = RequestMethod.POST)
	@ResponseBody
	public Object selfgrade(Integer bjId,Integer stuId,ModelMap modelMap) {
		List<Grade> grade=gradeService.findByTbClassIdAndStudnetId(bjId,stuId);
		modelMap.put("grade", grade);
		return grade;
	}
	
	//------------班级成绩查询-----------
	@RequestMapping(value = "bjChange")
	public String bjChange(Integer bjId,ModelMap modelMap) {
		List<Course> courses=gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> list=gradeService.findAllGradeByTbClassId(courses,bjId);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", list);
		return "grade/cjb";
		
		
	}
	//--------------保存-------------
	@RequestMapping(value = {"/save"}, method = RequestMethod.POST)
	@ResponseBody
	public Object save(GradeForm form) {
		Integer g=form.getGrade();
		Grade grade=gradeService.findById(form.getId());
		grade.setGrade(g);
		gradeService.save(grade);
		return true;	
	}
	//----------个人成绩查询-------------
	@RequestMapping(value = "/self")
	public String self(ModelMap modelMap) {
		SysUser user=userUtils.getUser();
		Integer id=user.getId();
		List<Grade> list=gradeService.findByStudentId(id);
		modelMap.put("list", list);
		return "grade/self";
	}
	//---------班级成绩查询----------------
	@RequestMapping(value = "/classcj")
	public String classcj(ModelMap modelMap) {
		SysUser user=userUtils.getUser();
		Integer id=user.getId();
		Student student=studentService.findById(id);
		Integer bjId=student.getTbClass().getId();
		List<Course> courses=gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> cjb= gradeService.getCjb(bjId,courses);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", cjb);
		modelMap.put("stu", student);
		return "grade/classcj";
	}
}
